package com.htbb.controller.pay;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.Map.Entry;

import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

/**
 * 海豚支付
 * @author wuyongde
 *
 */
public class HaiTunPayUtil {

	private final static Logger logger = LoggerFactory.getLogger(HaiTunPayUtil.class);
	
	private HaiTunPayUtil() {
	}

	/**
	 * 获取签名
	 * @param paramMap
	 * @param haitunKey
	 * @return
	 */
	public static String getSign(SortedMap<String, Object> paramMap, String haitunKey) {
		SortedMap<String, Object> resultMap = paramFilter(paramMap);
		String linkedStr = paramSign(resultMap, haitunKey);
		logger.info("*********************签名拼接串:" + linkedStr + haitunKey);
		String md5Hex = DigestUtils.md5Hex(linkedStr + haitunKey);
		return md5Hex;
	}
	
	/**
	 * 拼接form表单html
	 * @param paramMap
	 * @param config
	 * @return
	 */
	public static StringBuffer buildSubmitHtml(SortedMap<String, Object> paramMap, String haitunKey, String action) {
		paramMap.put("sign", getSign(paramMap, haitunKey));
		// 拼接form表单html
		StringBuffer sbHtml = new StringBuffer();
		sbHtml.append(
				"<form id=\"haitunsubmit\" name=\"haitunsubmit\" action=\"" + action + "\" method=\"post" + "\">");
		for (Iterator<Entry<String, Object>> it = paramMap.entrySet().iterator(); it.hasNext();) {
			Entry<String, Object> next = it.next();
			String key = next.getKey();
			Object value = next.getValue();
			if (value != null) {
				sbHtml.append("<input type=\"hidden\" name=\"" + key + "\" value=\"" + value + "\"/>");
			}
		}
		sbHtml.append("<input type=\"submit\" value=\"submit\" style=\"display:none;\"></form>");
		sbHtml.append("<script>document.forms['haitunsubmit'].submit();</script>");
		logger.info("*********************拼接form表单html:" + sbHtml);
		return sbHtml;
	}

	/**
	 * 将map值排序完成之后,再把所有数组值以“&”字符连接起来，最后MD5加密
	 * 
	 * @param resultMap
	 * @param appKey
	 * @return
	 */
	public static String paramSign(SortedMap<String, Object> resultMap, String appKey) {
		logger.info("*********************签名参数:" + resultMap);
		StringBuffer sb = new StringBuffer();
		List<String> keys = new ArrayList<String>(resultMap.keySet());
		for (int i = 0; i < keys.size(); i++) {
			String key = keys.get(i);
			Object value = resultMap.get(key);
			String html = i == keys.size() - 1 ? (key + "=" + value) : (key + "=" + value + "&");
			sb.append(html);
		}
		return sb.toString();
	}

	/**
	 * 去除map中一些不需要的值
	 * 
	 * @param paramMap
	 * @return
	 */
	public static SortedMap<String, Object> paramFilter(SortedMap<String, Object> paramMap) {
		SortedMap<String, Object> resultMap = new TreeMap<String, Object>();
		for (Iterator<Entry<String, Object>> it = paramMap.entrySet().iterator(); it.hasNext();) {
			Entry<String, Object> next = it.next();
			String key = next.getKey();
			if (StringUtils.isEmpty(next.getValue()) || "sign_type".equalsIgnoreCase(key) || "sign".equals(key)) {
				continue;
			} else {
				resultMap.put(key, next.getValue());
			}
		}
		return resultMap;
	}
}
